package com.practice.springboot_01_zwj.controller;

import com.practice.springboot_01_zwj.entity.AssignedTask;
import com.practice.springboot_01_zwj.entity.dto.AssignedTaskResponse;
import com.practice.springboot_01_zwj.entity.vo.R;
import com.practice.springboot_01_zwj.service.AssignedTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/assignedTask")
public class AssignedTaskController {

    @Autowired
    private AssignedTaskService assignedTaskService;

    @PostMapping("/addAssignedTask")
    public R<Integer> addAssignedTask(@RequestBody AssignedTask assignedTask) {
        try {
            int result = assignedTaskService.addAssignedTask(assignedTask);
            if (result > 0) {
                return R.success("添加成功", assignedTask.getId());
            } else {
                return R.error("添加失败");
            }
        } catch (Exception e) {
            return R.error("系统错误: " + e.getMessage());
        }
    }

    @PutMapping("/updateAssignedTask")
    public R<String> updateAssignedTask(@RequestBody AssignedTask assignedTask) {
        try {
            // 添加参数验证
            if (assignedTask.getId() == null) {
                return R.error(400, "更新失败：任务ID不能为空");
            }

            int result = assignedTaskService.updateAssignedTask(assignedTask);
            if (result > 0) {
                return R.success("更新成功", null);
            } else {
                return R.error("更新失败，可能原因：任务不存在或数据未变化");
            }
        } catch (Exception e) {
            return R.error("系统错误: " + e.getMessage());
        }
    }

    @DeleteMapping("/deleteAssignedTask/{id}")
    public R<String> deleteAssignedTask(@PathVariable Integer id) {
        try {
            int result = assignedTaskService.deleteAssignedTask(id);
            if (result > 0) {
                return R.success("删除成功", null);
            } else {
                return R.error("删除失败");
            }
        } catch (Exception e) {
            return R.error("系统错误: " + e.getMessage());
        }
    }

    @GetMapping("/selectAssignedTask")
    public R<AssignedTaskResponse> getAssignedTasks(
            @RequestParam(required = false) String proposeLeader,
            @RequestParam(required = false) String finishState,
            @RequestParam(required = false) String deptName,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        try {
            AssignedTaskResponse response = assignedTaskService.selectAssignedTask(
                    proposeLeader, finishState, deptName, page, size);
            return R.success("查询成功", response);
        } catch (Exception e) {
            return R.error("系统错误: " + e.getMessage());
        }
    }
    @GetMapping("/{id}")
    public R<AssignedTask> getAssignedTaskById(@PathVariable Integer id) {
        try {
            // 这里需要先在Service和Mapper中添加根据ID查询的方法
            // AssignedTask task = assignedTaskService.getAssignedTaskById(id);
            // if (task != null) {
            //     return R.success("查询成功", task);
            // } else {
            //     return R.error(404, "任务不存在");
            // }
            return R.error(501, "该方法尚未实现");
        } catch (Exception e) {
            return R.error("系统错误: " + e.getMessage());
        }
    }
}

